package top.carrotguo.weibo.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.sina.weibo.sdk.WbSdk;
import com.sina.weibo.sdk.auth.AccessTokenKeeper;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WbConnectErrorMessage;
import com.sina.weibo.sdk.auth.sso.SsoHandler;

import butterknife.OnClick;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.presenter.BasePresenter;
import top.carrotguo.weibo.ui.base.MVPBaseActivity;
import top.carrotguo.weibo.utils.Constants;

public class LoginActivity extends MVPBaseActivity {
    private AuthInfo mAuth;
    private Oauth2AccessToken mAccessToken;
    private SsoHandler mSsoHandler;

    @Override
    public int provideContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    public Bundle getBundleExtras(Bundle bundle) {
        return null;
    }

    @Override
    protected void initView() {
        super.initView();
        //创建授权对象
        mAuth = new AuthInfo(getApplicationContext(),
                Constants.APP_KEY,
                Constants.REDIRECT_URL,
                Constants.SCOPE);
        WbSdk.install(this,mAuth);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mSsoHandler != null) {
            mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

    @OnClick(R.id.btn_login)
    public void login(View view){
        mSsoHandler = new SsoHandler(LoginActivity.this);
        mSsoHandler.authorize(new SelfWbAuthListener());
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    private class SelfWbAuthListener implements com.sina.weibo.sdk.auth.WbAuthListener{
        @Override
        public void onSuccess(final Oauth2AccessToken token) {
            LoginActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mAccessToken = token;
                    if (mAccessToken.isSessionValid()) {
                        // 显示 Token
                        //pdateTokenView(false);
                        // 保存 Token 到 SharedPreferences
                        AccessTokenKeeper.writeAccessToken(LoginActivity.this, mAccessToken);
                        Toast.makeText(LoginActivity.this,
                                R.string.weibosdk_demo_toast_auth_success, Toast.LENGTH_SHORT).show();
                        Logger.d("accessToken:"+mAccessToken);
                    }
                }
            });
        }

        @Override
        public void cancel() {
            Toast.makeText(LoginActivity.this,
                    R.string.weibosdk_demo_toast_auth_canceled, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(WbConnectErrorMessage errorMessage) {
            Toast.makeText(LoginActivity.this, errorMessage.getErrorMessage(), Toast.LENGTH_LONG).show();
        }
    }
}
